package com.example.administrator.familfinalcialmanagement;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.example.administrator.familfinalcialmanagement.databinding.ActivityGrBinding;
import com.example.administrator.familfinalcialmanagement.ui.BaseActivity;
import com.example.administrator.familfinalcialmanagement.ui.adapter.BaseFragmentAdapter;
import com.example.administrator.familfinalcialmanagement.ui.fragment.IndexFragment;
import com.example.administrator.familfinalcialmanagement.ui.fragment.MessageFragment;
import com.example.administrator.familfinalcialmanagement.ui.fragment.MySelfFragment;
import com.example.administrator.familfinalcialmanagement.ui.fragment.ReasionFragment;
import com.example.administrator.familfinalcialmanagement.ui.fragment.SecondFragment;
import com.example.administrator.familfinalcialmanagement.ui.view.ApplicationState;
import com.example.administrator.familfinalcialmanagement.util.SpUtils;

/**
 * Created by Administrator on 2019/3/26.
 */

public class GRActivity extends BaseActivity implements ViewPager.OnPageChangeListener {

    ActivityGrBinding mainBinding;
    private ApplicationState applicationState = new ApplicationState();
    private int currentAt = 0;
    private Fragment fragment[] = new Fragment[5];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mainBinding = DataBindingUtil.setContentView(this, R.layout.activity_gr);
        mainBinding.setActivity(this);
        initView();
    }

    private void initView() {
        SpUtils.put("isLogin", "1");
        BaseFragmentAdapter fragmentAdapter = new BaseFragmentAdapter(getSupportFragmentManager());
        fragment[0] = new IndexFragment();
        fragment[1] = new SecondFragment();
        fragment[2] = new ReasionFragment();
        fragment[3] = new MessageFragment();
        fragment[4] = new MySelfFragment();
        fragmentAdapter.addData(fragment);
        mainBinding.viewPager.setAdapter(fragmentAdapter);
        mainBinding.viewPager.setCurrentItem(0);
        mainBinding.viewPager.setOnPageChangeListener(this);

        mainBinding.llModuleFirst.setOnClickListener(this);
        mainBinding.llModuleSecond.setOnClickListener(this);
        mainBinding.llModuleThird.setOnClickListener(this);
        mainBinding.llModuleForth.setOnClickListener(this);
        mainBinding.llModuleFifth.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.llModuleFirst:
                firstPoint();
                break;
            case R.id.llModuleSecond:
                secondPoint();
                break;
            case R.id.llModuleThird:
                thirdPoint();
                break;
            case R.id.llModuleForth:
                forthPoint();
                break;
            case R.id.llModuleFifth:
                fifthPoint();
                break;
            default:
        }
    }

    public void firstPoint() {
        mainBinding.viewPager.setCurrentItem(0);
        mainBinding.ivModuleFirst.setImageResource(R.drawable.icon_first_after);
        mainBinding.ivModuleSecond.setImageResource(R.drawable.icon_second_before);
        mainBinding.ivModuleThird.setImageResource(R.drawable.icon_third_before);
        mainBinding.ivModuleForth.setImageResource(R.drawable.icon_second_before);
        mainBinding.ivModuleFifth.setImageResource(R.drawable.icon_forth_before);

    }

    public void secondPoint() {
        mainBinding.viewPager.setCurrentItem(1);
        mainBinding.ivModuleFirst.setImageResource(R.drawable.icon_first_before);
        mainBinding.ivModuleSecond.setImageResource(R.drawable.icon_second_after);
        mainBinding.ivModuleThird.setImageResource(R.drawable.icon_third_before);
        mainBinding.ivModuleForth.setImageResource(R.drawable.icon_second_before);
        mainBinding.ivModuleFifth.setImageResource(R.drawable.icon_forth_before);
    }

    public void thirdPoint() {
        mainBinding.viewPager.setCurrentItem(2);
        mainBinding.ivModuleFirst.setImageResource(R.drawable.icon_first_before);
        mainBinding.ivModuleSecond.setImageResource(R.drawable.icon_second_before);
        mainBinding.ivModuleThird.setImageResource(R.drawable.icon_third_after);
        mainBinding.ivModuleForth.setImageResource(R.drawable.icon_second_before);
        mainBinding.ivModuleFifth.setImageResource(R.drawable.icon_forth_before);
    }

    public void forthPoint() {
        mainBinding.viewPager.setCurrentItem(3);
        mainBinding.ivModuleFirst.setImageResource(R.drawable.icon_first_before);
        mainBinding.ivModuleSecond.setImageResource(R.drawable.icon_second_before);
        mainBinding.ivModuleThird.setImageResource(R.drawable.icon_third_before);
        mainBinding.ivModuleForth.setImageResource(R.drawable.icon_second_after);
        mainBinding.ivModuleFifth.setImageResource(R.drawable.icon_forth_before);
    }

    public void fifthPoint(){
        mainBinding.viewPager.setCurrentItem(4);
        mainBinding.ivModuleFirst.setImageResource(R.drawable.icon_first_before);
        mainBinding.ivModuleSecond.setImageResource(R.drawable.icon_second_before);
        mainBinding.ivModuleThird.setImageResource(R.drawable.icon_third_before);
        mainBinding.ivModuleForth.setImageResource(R.drawable.icon_second_before);
        mainBinding.ivModuleFifth.setImageResource(R.drawable.icon_forth_after);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if (applicationState.isMainSliding() && position == 1) {
            applicationState.setMainSliding(false);
        }
        switch (position) {
            case 0:
                firstPoint();
                break;
            case 1:
                secondPoint();
                break;
            case 2:
                thirdPoint();
                break;
            case 3:
                forthPoint();
                break;
            case 4:
                fifthPoint();
                break;
            default:
        }

    }

    @Override
    public void onPageScrollStateChanged(int state) {
        if (!applicationState.isMainSliding() && state != ViewPager.SCROLL_STATE_IDLE) {
            applicationState.setMainSliding(true);
        }
    }
}

